/*
 * PFArrayBak.cpp
 *
 *  Created on: Mar 1, 2014
 *      Author: Anthony Varghese - This is from the Savitch book
 */

#include "PFArrayBak.h"
#include "PFArray.cpp"

#include <iostream>
using std::cout;

namespace PFArraySavitch {

template<class T>
PFArrayBak<T>::PFArrayBak() :
		PFArray<T>(), usedB(0) {
	int cap = PFArray<T>::getCapacity();
	b = new T[cap];
}

template<class T>
PFArrayBak<T>::PFArrayBak(int capacityValue) :
		PFArray<T>(capacityValue), usedB(0) {
	int cap = PFArray<T>::getCapacity();
	b = new T[cap];
}

template<class T>
PFArrayBak<T>::PFArrayBak(const PFArrayBak<T>& oldObject) :
		PFArray<T>(oldObject), usedB(0) {
	int cap = PFArray<T>::getCapacity();
	b = new T[cap];
	usedB = oldObject.getNumberUsed();
	for (int i = 0; i < usedB; i++)
		b[i] = oldObject.b[i];
}

template<class T>
void PFArrayBak<T>::backup() {
	usedB = PFArray<T>::getNumberUsed();
	for (int i = 0; i < usedB; i++)
		b[i] = PFArray<T>::operator[](i);
}

template<class T>
void PFArrayBak<T>::restore() {
	PFArray<T>::emptyArray();

	for (int i = 0; i < usedB; i++)
		PFArray<T>::addElement (b[i]);
}

template<class T>
PFArrayBak<T>& PFArrayBak<T>::operator =(const PFArrayBak<T>& rightSide) {
	PFArray < T > ::operator =(rightSide);

	if (PFArray<T>::getCapacity() != rightSide.getCapacity()) {
		delete[] b;
		b = new T[rightSide.getCapacity()];
	}

	usedB = rightSide.usedB;
	for (int i = 0; i < usedB; i++)
		b[i] = rightSide.b[i];

	return *this;
}

template<class T>
PFArrayBak<T>::~PFArrayBak() {
	delete[] b;
}

} /* namespace PFArraySavitch */
